{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import csv\n",
    "import time\n",
    "import pandas as pd\n",
    "pro = ts.pro_api('35d8848b876df93910413e8936c40745d7b7da42553ae73920862cd9')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = '20200101'\n",
    "end_date = '20200301'\n",
    "ts_code = '000001.SZ'\n",
    "year='2020'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 股票数据获取（Tushare）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1 股票基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>TS代码</th>\n",
       "      <th>股票代码</th>\n",
       "      <th>股票名称</th>\n",
       "      <th>行业</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>000001</td>\n",
       "      <td>平安银行</td>\n",
       "      <td>银行</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>000002</td>\n",
       "      <td>万科A</td>\n",
       "      <td>全国地产</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>000004</td>\n",
       "      <td>国华网安</td>\n",
       "      <td>软件服务</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000005.SZ</td>\n",
       "      <td>000005</td>\n",
       "      <td>ST星源</td>\n",
       "      <td>环境保护</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000006.SZ</td>\n",
       "      <td>000006</td>\n",
       "      <td>深振业A</td>\n",
       "      <td>区域地产</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        TS代码    股票代码  股票名称    行业\n",
       "0  000001.SZ  000001  平安银行    银行\n",
       "1  000002.SZ  000002   万科A  全国地产\n",
       "2  000004.SZ  000004  国华网安  软件服务\n",
       "3  000005.SZ  000005  ST星源  环境保护\n",
       "4  000006.SZ  000006  深振业A  区域地产"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_basic = pro.stock_basic(list_status='L', fields='ts_code, symbol, name, industry')\n",
    "\n",
    "# 重命名字段（便于后续导入neo4j）\n",
    "basic_rename = {'ts_code': 'TS代码', 'symbol': '股票代码', 'name': '股票名称', 'industry': '行业'}\n",
    "stock_basic.rename(columns=basic_rename, inplace=True)\n",
    "\n",
    "# 保存为stock_basic.csv\n",
    "stock_basic.to_csv('financial_data\\\\stock_basic.csv', encoding='gbk')\n",
    "\n",
    "stock_basic.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2 股票Top10股东信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>ann_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>holder_name</th>\n",
       "      <th>hold_amount</th>\n",
       "      <th>hold_ratio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200421</td>\n",
       "      <td>20200331</td>\n",
       "      <td>中国平安保险(集团)股份有限公司-集团本级-自有资金</td>\n",
       "      <td>9.618540e+09</td>\n",
       "      <td>49.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200421</td>\n",
       "      <td>20200331</td>\n",
       "      <td>香港中央结算有限公司(陆股通)</td>\n",
       "      <td>1.669875e+09</td>\n",
       "      <td>8.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200421</td>\n",
       "      <td>20200331</td>\n",
       "      <td>中国平安人寿保险股份有限公司-自有资金</td>\n",
       "      <td>1.186100e+09</td>\n",
       "      <td>6.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200421</td>\n",
       "      <td>20200331</td>\n",
       "      <td>中国平安人寿保险股份有限公司-传统-普通保险产品</td>\n",
       "      <td>4.404787e+08</td>\n",
       "      <td>2.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200421</td>\n",
       "      <td>20200331</td>\n",
       "      <td>中国证券金融股份有限公司</td>\n",
       "      <td>4.292327e+08</td>\n",
       "      <td>2.21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code  ann_date  end_date                 holder_name   hold_amount  \\\n",
       "0  000001.SZ  20200421  20200331  中国平安保险(集团)股份有限公司-集团本级-自有资金  9.618540e+09   \n",
       "1  000001.SZ  20200421  20200331             香港中央结算有限公司(陆股通)  1.669875e+09   \n",
       "2  000001.SZ  20200421  20200331         中国平安人寿保险股份有限公司-自有资金  1.186100e+09   \n",
       "3  000001.SZ  20200421  20200331    中国平安人寿保险股份有限公司-传统-普通保险产品  4.404787e+08   \n",
       "4  000001.SZ  20200421  20200331                中国证券金融股份有限公司  4.292327e+08   \n",
       "\n",
       "   hold_ratio  \n",
       "0       49.56  \n",
       "1        8.60  \n",
       "2        6.11  \n",
       "3        2.27  \n",
       "4        2.21  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "holders = pd.DataFrame(columns=('ts_code', 'ann_date', 'end_date', 'holder_name', 'hold_amount', 'hold_ratio'))\n",
    "\n",
    "top10_holders = pro.top10_holders(ts_code=ts_code, start_date=start_date, end_date=end_date)\n",
    "\n",
    "top10_holders.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.3 股票概念信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "concept_details = pd.DataFrame(columns=('id', 'concept_name', 'ts_code', 'name'))\n",
    "\n",
    "# concept_detail = pro.concept_detail(id='TS0')  # 该接口 TOKEN 受限\n",
    "    \n",
    "# concept_detail.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.4 股票公告信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# notices = pro.anns(ts_code=ts_code, start_date=start_date, end_date=end_date, year=year)  # 该接口 TOKEN 受限\n",
    "\n",
    "# notices.to_csv(\"financial_data\\\\notices\\\\\"+str(code)+\".csv\",encoding='utf_8_sig',index=False)\n",
    "\n",
    "# notices.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.5 财经新闻信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>content</th>\n",
       "      <th>title</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-11-22 10:09:00</td>\n",
       "      <td>【北京：10月末非金融企业贷款增速创历史同期新高 】据中国人民银行营业管理部11月22日披露...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-11-22 10:08:29</td>\n",
       "      <td>【湖北省积极推动企业向科创板靠拢，年底将向上交所推荐一批优质企业】湖北省上市指导中心相关人士...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-11-22 10:08:04</td>\n",
       "      <td>【上海：加快专利审查速度 进一步缩短专利授权周期】上海市人民政府发布关于加快本市高新技术企业...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-11-22 10:04:35</td>\n",
       "      <td>【生意社：稀土价格走势或将上涨】11月20日稀土指数为344点，与上一日持平，较2011年1...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-11-22 10:03:40</td>\n",
       "      <td>【上海：加快建设具有全球影响力的科技创新中心 做大做强高新技术产业】上海市人民政府发布关于加...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime                                            content  \\\n",
       "0  2018-11-22 10:09:00  【北京：10月末非金融企业贷款增速创历史同期新高 】据中国人民银行营业管理部11月22日披露...   \n",
       "1  2018-11-22 10:08:29  【湖北省积极推动企业向科创板靠拢，年底将向上交所推荐一批优质企业】湖北省上市指导中心相关人士...   \n",
       "2  2018-11-22 10:08:04  【上海：加快专利审查速度 进一步缩短专利授权周期】上海市人民政府发布关于加快本市高新技术企业...   \n",
       "3  2018-11-22 10:04:35  【生意社：稀土价格走势或将上涨】11月20日稀土指数为344点，与上一日持平，较2011年1...   \n",
       "4  2018-11-22 10:03:40  【上海：加快建设具有全球影响力的科技创新中心 做大做强高新技术产业】上海市人民政府发布关于加...   \n",
       "\n",
       "  title  \n",
       "0        \n",
       "1        \n",
       "2        \n",
       "3        \n",
       "4        "
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "news = pro.news(src='sina', start_date='2018-11-21 09:00:00', end_date='2018-11-22 10:10:00')\n",
    "news.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.6 概念信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# concept = pro.concept()  # 该接口 TOKEN 受限\n",
    "\n",
    "# concept.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.7 沪股通成分和深股通成分信息 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>hs_type</th>\n",
       "      <th>in_date</th>\n",
       "      <th>out_date</th>\n",
       "      <th>is_new</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601628.SH</td>\n",
       "      <td>SH</td>\n",
       "      <td>20141117</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>601099.SH</td>\n",
       "      <td>SH</td>\n",
       "      <td>20141117</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>601808.SH</td>\n",
       "      <td>SH</td>\n",
       "      <td>20141117</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>601107.SH</td>\n",
       "      <td>SH</td>\n",
       "      <td>20141117</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>601880.SH</td>\n",
       "      <td>SH</td>\n",
       "      <td>20141117</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code hs_type   in_date out_date is_new\n",
       "0  601628.SH      SH  20141117     None      1\n",
       "1  601099.SH      SH  20141117     None      1\n",
       "2  601808.SH      SH  20141117     None      1\n",
       "3  601107.SH      SH  20141117     None      1\n",
       "4  601880.SH      SH  20141117     None      1"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sh = pro.hs_const(hs_type='SH')  # 获取沪股通成分\n",
    "\n",
    "sz = pro.hs_const(hs_type='SZ')  # 获取深股通成分\n",
    "\n",
    "sh.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.8 股票价格信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200123</td>\n",
       "      <td>15.92</td>\n",
       "      <td>15.92</td>\n",
       "      <td>15.39</td>\n",
       "      <td>15.54</td>\n",
       "      <td>16.09</td>\n",
       "      <td>-0.55</td>\n",
       "      <td>-3.4183</td>\n",
       "      <td>1100592.07</td>\n",
       "      <td>1723394.336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200122</td>\n",
       "      <td>15.92</td>\n",
       "      <td>16.16</td>\n",
       "      <td>15.71</td>\n",
       "      <td>16.09</td>\n",
       "      <td>16.00</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.5625</td>\n",
       "      <td>719464.91</td>\n",
       "      <td>1150933.398</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200121</td>\n",
       "      <td>16.34</td>\n",
       "      <td>16.34</td>\n",
       "      <td>15.93</td>\n",
       "      <td>16.00</td>\n",
       "      <td>16.45</td>\n",
       "      <td>-0.45</td>\n",
       "      <td>-2.7356</td>\n",
       "      <td>896603.10</td>\n",
       "      <td>1442171.431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200120</td>\n",
       "      <td>16.43</td>\n",
       "      <td>16.61</td>\n",
       "      <td>16.35</td>\n",
       "      <td>16.45</td>\n",
       "      <td>16.39</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.3661</td>\n",
       "      <td>746074.75</td>\n",
       "      <td>1226464.649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200117</td>\n",
       "      <td>16.38</td>\n",
       "      <td>16.55</td>\n",
       "      <td>16.35</td>\n",
       "      <td>16.39</td>\n",
       "      <td>16.33</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.3674</td>\n",
       "      <td>605436.69</td>\n",
       "      <td>995909.007</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "0  000001.SZ   20200123  15.92  15.92  15.39  15.54      16.09   -0.55   \n",
       "1  000001.SZ   20200122  15.92  16.16  15.71  16.09      16.00    0.09   \n",
       "2  000001.SZ   20200121  16.34  16.34  15.93  16.00      16.45   -0.45   \n",
       "3  000001.SZ   20200120  16.43  16.61  16.35  16.45      16.39    0.06   \n",
       "4  000001.SZ   20200117  16.38  16.55  16.35  16.39      16.33    0.06   \n",
       "\n",
       "   pct_chg         vol       amount  \n",
       "0  -3.4183  1100592.07  1723394.336  \n",
       "1   0.5625   719464.91  1150933.398  \n",
       "2  -2.7356   896603.10  1442171.431  \n",
       "3   0.3661   746074.75  1226464.649  \n",
       "4   0.3674   605436.69   995909.007  "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "end_date = '20200201'  # 使用更短数据目的是1.9绘制走势图数值不重叠\n",
    "\n",
    "price = pro.query('daily', ts_code=ts_code, start_date=start_date, end_date=end_date)\n",
    "\n",
    "price.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.9 股票价格走势图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"c3efa850753d482ba3381089121d31fc\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_c3efa850753d482ba3381089121d31fc = echarts.init(\n",
       "                    document.getElementById('c3efa850753d482ba3381089121d31fc'), 'white', {renderer: 'canvas'});\n",
       "                var option_c3efa850753d482ba3381089121d31fc = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"name\": \"\\u6536\\u76d8\\u4ef7\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbol\": \"circle\",\n",
       "            \"symbolSize\": 4,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    \"20200102\",\n",
       "                    16.87\n",
       "                ],\n",
       "                [\n",
       "                    \"20200103\",\n",
       "                    17.18\n",
       "                ],\n",
       "                [\n",
       "                    \"20200106\",\n",
       "                    17.07\n",
       "                ],\n",
       "                [\n",
       "                    \"20200107\",\n",
       "                    17.15\n",
       "                ],\n",
       "                [\n",
       "                    \"20200108\",\n",
       "                    16.66\n",
       "                ],\n",
       "                [\n",
       "                    \"20200109\",\n",
       "                    16.79\n",
       "                ],\n",
       "                [\n",
       "                    \"20200110\",\n",
       "                    16.69\n",
       "                ],\n",
       "                [\n",
       "                    \"20200113\",\n",
       "                    16.99\n",
       "                ],\n",
       "                [\n",
       "                    \"20200114\",\n",
       "                    16.76\n",
       "                ],\n",
       "                [\n",
       "                    \"20200115\",\n",
       "                    16.52\n",
       "                ],\n",
       "                [\n",
       "                    \"20200116\",\n",
       "                    16.33\n",
       "                ],\n",
       "                [\n",
       "                    \"20200117\",\n",
       "                    16.39\n",
       "                ],\n",
       "                [\n",
       "                    \"20200120\",\n",
       "                    16.45\n",
       "                ],\n",
       "                [\n",
       "                    \"20200121\",\n",
       "                    16.0\n",
       "                ],\n",
       "                [\n",
       "                    \"20200122\",\n",
       "                    16.09\n",
       "                ],\n",
       "                [\n",
       "                    \"20200123\",\n",
       "                    15.54\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u6536\\u76d8\\u4ef7\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u6536\\u76d8\\u4ef7\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                \"20200102\",\n",
       "                \"20200103\",\n",
       "                \"20200106\",\n",
       "                \"20200107\",\n",
       "                \"20200108\",\n",
       "                \"20200109\",\n",
       "                \"20200110\",\n",
       "                \"20200113\",\n",
       "                \"20200114\",\n",
       "                \"20200115\",\n",
       "                \"20200116\",\n",
       "                \"20200117\",\n",
       "                \"20200120\",\n",
       "                \"20200121\",\n",
       "                \"20200122\",\n",
       "                \"20200123\"\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u4ef7\\u683c\\u8d70\\u52bf\\u56fe\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_c3efa850753d482ba3381089121d31fc.setOption(option_c3efa850753d482ba3381089121d31fc);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x7ffb145fbc50>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyecharts.charts import Line\n",
    "from pyecharts import options as opts\n",
    "import numpy as np\n",
    "\n",
    "price = pro.query('daily', ts_code=ts_code, start_date=start_date, end_date=end_date)\n",
    "(\n",
    "    Line()\n",
    "    .add_xaxis(xaxis_data=list(price['trade_date'])[::-1])\n",
    "    .add_yaxis(series_name=\"收盘价\",y_axis=list(price['close'])[::-1],symbol=\"circle\")\n",
    "#     .add_yaxis(series_name=\"开盘价\",y_axis=list(price['open'])[::-1],symbol=\"circle\")\n",
    "#     .add_yaxis(series_name=\"最高价\",y_axis=list(price['high'])[::-1],symbol=\"circle\")\n",
    "#     .add_yaxis(series_name=\"最低价\",y_axis=list(price['low'])[::-1],symbol=\"circle\")\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"价格走势图\"))\n",
    "    .render_notebook()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.10 基本面数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>chairman</th>\n",
       "      <th>manager</th>\n",
       "      <th>secretary</th>\n",
       "      <th>reg_capital</th>\n",
       "      <th>setup_date</th>\n",
       "      <th>province</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>300952.SZ</td>\n",
       "      <td>王咸华</td>\n",
       "      <td>王咸华</td>\n",
       "      <td>张武芬</td>\n",
       "      <td>14492.7653</td>\n",
       "      <td>20040415</td>\n",
       "      <td>江苏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>300268.SZ</td>\n",
       "      <td>汤捷</td>\n",
       "      <td>汤捷</td>\n",
       "      <td>杨振刚</td>\n",
       "      <td>17420.0000</td>\n",
       "      <td>20030508</td>\n",
       "      <td>湖南</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>300447.SZ</td>\n",
       "      <td>陈祥楼</td>\n",
       "      <td>何亮</td>\n",
       "      <td>孙璐</td>\n",
       "      <td>29106.9255</td>\n",
       "      <td>20010929</td>\n",
       "      <td>江苏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>300451.SZ</td>\n",
       "      <td>葛航</td>\n",
       "      <td>张吕峥</td>\n",
       "      <td>胡燕</td>\n",
       "      <td>119260.8104</td>\n",
       "      <td>19971210</td>\n",
       "      <td>浙江</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>002531.SZ</td>\n",
       "      <td>严俊旭</td>\n",
       "      <td>严俊旭</td>\n",
       "      <td>朱彬</td>\n",
       "      <td>180250.9062</td>\n",
       "      <td>20050118</td>\n",
       "      <td>江苏</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code chairman manager secretary  reg_capital setup_date province\n",
       "0  300952.SZ      王咸华     王咸华       张武芬   14492.7653   20040415       江苏\n",
       "1  300268.SZ       汤捷      汤捷       杨振刚   17420.0000   20030508       湖南\n",
       "2  300447.SZ      陈祥楼      何亮        孙璐   29106.9255   20010929       江苏\n",
       "3  300451.SZ       葛航     张吕峥        胡燕  119260.8104   19971210       浙江\n",
       "4  002531.SZ      严俊旭     严俊旭        朱彬  180250.9062   20050118       江苏"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_company = pro.stock_company(exchange='SZSE', fields='ts_code,chairman,manager,secretary,reg_capital,setup_date,province')\n",
    "stock_company.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>type</th>\n",
       "      <th>date</th>\n",
       "      <th>url</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>平安银行：平安银行股份有限公司2020年年度权益分派实施公告2021-05-07</td>\n",
       "      <td>临时公告</td>\n",
       "      <td>2021-05-07</td>\n",
       "      <td>http://vip.stock.finance.sina.com.cn/corp/view...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>平安银行：2021年第一季度报告全文</td>\n",
       "      <td>一季度报告</td>\n",
       "      <td>2021-04-21</td>\n",
       "      <td>http://vip.stock.finance.sina.com.cn/corp/view...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>平安银行：一季报监事会决议公告</td>\n",
       "      <td>临时公告</td>\n",
       "      <td>2021-04-21</td>\n",
       "      <td>http://vip.stock.finance.sina.com.cn/corp/view...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>平安银行：一季报董事会决议公告</td>\n",
       "      <td>临时公告</td>\n",
       "      <td>2021-04-21</td>\n",
       "      <td>http://vip.stock.finance.sina.com.cn/corp/view...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>平安银行：2021年第一季度报告正文</td>\n",
       "      <td>一季度报告（摘要）</td>\n",
       "      <td>2021-04-21</td>\n",
       "      <td>http://vip.stock.finance.sina.com.cn/corp/view...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      title       type        date  \\\n",
       "0  平安银行：平安银行股份有限公司2020年年度权益分派实施公告2021-05-07       临时公告  2021-05-07   \n",
       "1                        平安银行：2021年第一季度报告全文      一季度报告  2021-04-21   \n",
       "2                           平安银行：一季报监事会决议公告       临时公告  2021-04-21   \n",
       "3                           平安银行：一季报董事会决议公告       临时公告  2021-04-21   \n",
       "4                        平安银行：2021年第一季度报告正文  一季度报告（摘要）  2021-04-21   \n",
       "\n",
       "                                                 url  \n",
       "0  http://vip.stock.finance.sina.com.cn/corp/view...  \n",
       "1  http://vip.stock.finance.sina.com.cn/corp/view...  \n",
       "2  http://vip.stock.finance.sina.com.cn/corp/view...  \n",
       "3  http://vip.stock.finance.sina.com.cn/corp/view...  \n",
       "4  http://vip.stock.finance.sina.com.cn/corp/view...  "
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tushare as ts\n",
    "ts.get_notices(\"000001\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>ptime</th>\n",
       "      <th>rcounts</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>机构减仓或在战役性转移</td>\n",
       "      <td>05月07日 14:57</td>\n",
       "      <td>360.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>170家电子行业公司发布年度业绩预告</td>\n",
       "      <td></td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>券商开户专属通道：新客专享理财福利多多</td>\n",
       "      <td></td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>热门股票如何选择盘中介入点</td>\n",
       "      <td>05月07日 07:40</td>\n",
       "      <td>419.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>节后综合症再现 依然不乏结构性机会</td>\n",
       "      <td>05月07日 08:03</td>\n",
       "      <td>507.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 title         ptime  rcounts\n",
       "0          机构减仓或在战役性转移  05月07日 14:57    360.0\n",
       "1   170家电子行业公司发布年度业绩预告                    0.0\n",
       "2  券商开户专属通道：新客专享理财福利多多                    0.0\n",
       "3        热门股票如何选择盘中介入点  05月07日 07:40    419.0\n",
       "4    节后综合症再现 依然不乏结构性机会  05月07日 08:03    507.0"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.guba_sina().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-05-07</th>\n",
       "      <td>23.67</td>\n",
       "      <td>24.30</td>\n",
       "      <td>24.05</td>\n",
       "      <td>23.39</td>\n",
       "      <td>802214.25</td>\n",
       "      <td>0.55</td>\n",
       "      <td>2.34</td>\n",
       "      <td>23.556</td>\n",
       "      <td>23.294</td>\n",
       "      <td>22.153</td>\n",
       "      <td>614633.11</td>\n",
       "      <td>766603.84</td>\n",
       "      <td>700531.86</td>\n",
       "      <td>0.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-06</th>\n",
       "      <td>23.10</td>\n",
       "      <td>23.70</td>\n",
       "      <td>23.50</td>\n",
       "      <td>23.10</td>\n",
       "      <td>500295.19</td>\n",
       "      <td>0.21</td>\n",
       "      <td>0.90</td>\n",
       "      <td>23.334</td>\n",
       "      <td>23.058</td>\n",
       "      <td>22.034</td>\n",
       "      <td>548276.55</td>\n",
       "      <td>769859.69</td>\n",
       "      <td>680476.29</td>\n",
       "      <td>0.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-30</th>\n",
       "      <td>23.35</td>\n",
       "      <td>23.49</td>\n",
       "      <td>23.29</td>\n",
       "      <td>23.01</td>\n",
       "      <td>561981.31</td>\n",
       "      <td>-0.30</td>\n",
       "      <td>-1.27</td>\n",
       "      <td>23.222</td>\n",
       "      <td>22.823</td>\n",
       "      <td>21.934</td>\n",
       "      <td>622701.05</td>\n",
       "      <td>831058.44</td>\n",
       "      <td>684515.71</td>\n",
       "      <td>0.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-29</th>\n",
       "      <td>23.34</td>\n",
       "      <td>23.71</td>\n",
       "      <td>23.59</td>\n",
       "      <td>23.11</td>\n",
       "      <td>614836.88</td>\n",
       "      <td>0.24</td>\n",
       "      <td>1.03</td>\n",
       "      <td>23.222</td>\n",
       "      <td>22.520</td>\n",
       "      <td>21.859</td>\n",
       "      <td>674950.82</td>\n",
       "      <td>847860.13</td>\n",
       "      <td>683639.09</td>\n",
       "      <td>0.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-28</th>\n",
       "      <td>23.29</td>\n",
       "      <td>23.45</td>\n",
       "      <td>23.35</td>\n",
       "      <td>22.78</td>\n",
       "      <td>593837.94</td>\n",
       "      <td>0.41</td>\n",
       "      <td>1.79</td>\n",
       "      <td>23.100</td>\n",
       "      <td>22.197</td>\n",
       "      <td>21.780</td>\n",
       "      <td>717922.23</td>\n",
       "      <td>859962.95</td>\n",
       "      <td>691748.65</td>\n",
       "      <td>0.31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high  close    low     volume  price_change  p_change  \\\n",
       "date                                                                        \n",
       "2021-05-07  23.67  24.30  24.05  23.39  802214.25          0.55      2.34   \n",
       "2021-05-06  23.10  23.70  23.50  23.10  500295.19          0.21      0.90   \n",
       "2021-04-30  23.35  23.49  23.29  23.01  561981.31         -0.30     -1.27   \n",
       "2021-04-29  23.34  23.71  23.59  23.11  614836.88          0.24      1.03   \n",
       "2021-04-28  23.29  23.45  23.35  22.78  593837.94          0.41      1.79   \n",
       "\n",
       "               ma5    ma10    ma20      v_ma5     v_ma10     v_ma20  turnover  \n",
       "date                                                                           \n",
       "2021-05-07  23.556  23.294  22.153  614633.11  766603.84  700531.86      0.41  \n",
       "2021-05-06  23.334  23.058  22.034  548276.55  769859.69  680476.29      0.26  \n",
       "2021-04-30  23.222  22.823  21.934  622701.05  831058.44  684515.71      0.29  \n",
       "2021-04-29  23.222  22.520  21.859  674950.82  847860.13  683639.09      0.32  \n",
       "2021-04-28  23.100  22.197  21.780  717922.23  859962.95  691748.65      0.31  "
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.get_hist_data(\"000001\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-05-07</th>\n",
       "      <td>23.10</td>\n",
       "      <td>24.30</td>\n",
       "      <td>24.05</td>\n",
       "      <td>23.10</td>\n",
       "      <td>1302509.50</td>\n",
       "      <td>0.76</td>\n",
       "      <td>3.26</td>\n",
       "      <td>22.438</td>\n",
       "      <td>21.912</td>\n",
       "      <td>21.823</td>\n",
       "      <td>2882347.97</td>\n",
       "      <td>3893787.24</td>\n",
       "      <td>4866394.11</td>\n",
       "      <td>0.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-30</th>\n",
       "      <td>23.87</td>\n",
       "      <td>24.23</td>\n",
       "      <td>23.29</td>\n",
       "      <td>22.78</td>\n",
       "      <td>3113505.25</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>21.928</td>\n",
       "      <td>21.645</td>\n",
       "      <td>21.538</td>\n",
       "      <td>3307222.47</td>\n",
       "      <td>4560954.59</td>\n",
       "      <td>5004209.48</td>\n",
       "      <td>1.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-23</th>\n",
       "      <td>20.03</td>\n",
       "      <td>23.65</td>\n",
       "      <td>23.29</td>\n",
       "      <td>19.91</td>\n",
       "      <td>5197079.00</td>\n",
       "      <td>3.03</td>\n",
       "      <td>14.96</td>\n",
       "      <td>21.498</td>\n",
       "      <td>21.701</td>\n",
       "      <td>21.300</td>\n",
       "      <td>3544311.92</td>\n",
       "      <td>4492385.46</td>\n",
       "      <td>5036850.99</td>\n",
       "      <td>2.68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-16</th>\n",
       "      <td>21.51</td>\n",
       "      <td>21.51</td>\n",
       "      <td>20.26</td>\n",
       "      <td>19.81</td>\n",
       "      <td>3097949.00</td>\n",
       "      <td>-1.04</td>\n",
       "      <td>-4.88</td>\n",
       "      <td>20.934</td>\n",
       "      <td>21.754</td>\n",
       "      <td>21.100</td>\n",
       "      <td>3492326.92</td>\n",
       "      <td>4367200.06</td>\n",
       "      <td>5044014.37</td>\n",
       "      <td>1.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-09</th>\n",
       "      <td>21.55</td>\n",
       "      <td>22.09</td>\n",
       "      <td>21.30</td>\n",
       "      <td>21.08</td>\n",
       "      <td>1700697.12</td>\n",
       "      <td>-0.20</td>\n",
       "      <td>-0.93</td>\n",
       "      <td>21.178</td>\n",
       "      <td>22.221</td>\n",
       "      <td>21.072</td>\n",
       "      <td>3922926.32</td>\n",
       "      <td>4865955.41</td>\n",
       "      <td>5078637.83</td>\n",
       "      <td>0.88</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high  close    low      volume  price_change  p_change  \\\n",
       "date                                                                         \n",
       "2021-05-07  23.10  24.30  24.05  23.10  1302509.50          0.76      3.26   \n",
       "2021-04-30  23.87  24.23  23.29  22.78  3113505.25          0.00      0.00   \n",
       "2021-04-23  20.03  23.65  23.29  19.91  5197079.00          3.03     14.96   \n",
       "2021-04-16  21.51  21.51  20.26  19.81  3097949.00         -1.04     -4.88   \n",
       "2021-04-09  21.55  22.09  21.30  21.08  1700697.12         -0.20     -0.93   \n",
       "\n",
       "               ma5    ma10    ma20       v_ma5      v_ma10      v_ma20  \\\n",
       "date                                                                     \n",
       "2021-05-07  22.438  21.912  21.823  2882347.97  3893787.24  4866394.11   \n",
       "2021-04-30  21.928  21.645  21.538  3307222.47  4560954.59  5004209.48   \n",
       "2021-04-23  21.498  21.701  21.300  3544311.92  4492385.46  5036850.99   \n",
       "2021-04-16  20.934  21.754  21.100  3492326.92  4367200.06  5044014.37   \n",
       "2021-04-09  21.178  22.221  21.072  3922926.32  4865955.41  5078637.83   \n",
       "\n",
       "            turnover  \n",
       "date                  \n",
       "2021-05-07      0.67  \n",
       "2021-04-30      1.60  \n",
       "2021-04-23      2.68  \n",
       "2021-04-16      1.60  \n",
       "2021-04-09      0.88  "
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.get_hist_data(\"000001\",ktype=\"w\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-05-07</th>\n",
       "      <td>23.10</td>\n",
       "      <td>24.30</td>\n",
       "      <td>24.05</td>\n",
       "      <td>23.10</td>\n",
       "      <td>1302509.5</td>\n",
       "      <td>0.76</td>\n",
       "      <td>3.26</td>\n",
       "      <td>22.764</td>\n",
       "      <td>20.090</td>\n",
       "      <td>17.241</td>\n",
       "      <td>17859144.9</td>\n",
       "      <td>20308849.85</td>\n",
       "      <td>20748658.73</td>\n",
       "      <td>0.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-30</th>\n",
       "      <td>22.08</td>\n",
       "      <td>24.23</td>\n",
       "      <td>23.29</td>\n",
       "      <td>19.81</td>\n",
       "      <td>14234763.0</td>\n",
       "      <td>1.28</td>\n",
       "      <td>5.82</td>\n",
       "      <td>21.822</td>\n",
       "      <td>19.019</td>\n",
       "      <td>16.818</td>\n",
       "      <td>21521885.8</td>\n",
       "      <td>24854576.10</td>\n",
       "      <td>21952860.65</td>\n",
       "      <td>7.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-31</th>\n",
       "      <td>21.54</td>\n",
       "      <td>23.49</td>\n",
       "      <td>22.01</td>\n",
       "      <td>20.28</td>\n",
       "      <td>23400600.0</td>\n",
       "      <td>0.63</td>\n",
       "      <td>2.95</td>\n",
       "      <td>21.112</td>\n",
       "      <td>17.970</td>\n",
       "      <td>16.361</td>\n",
       "      <td>22789929.6</td>\n",
       "      <td>25258599.60</td>\n",
       "      <td>22737767.70</td>\n",
       "      <td>12.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-02-26</th>\n",
       "      <td>23.00</td>\n",
       "      <td>25.31</td>\n",
       "      <td>21.38</td>\n",
       "      <td>21.21</td>\n",
       "      <td>22432726.0</td>\n",
       "      <td>-1.71</td>\n",
       "      <td>-7.41</td>\n",
       "      <td>20.260</td>\n",
       "      <td>17.069</td>\n",
       "      <td>15.967</td>\n",
       "      <td>22277667.2</td>\n",
       "      <td>24135001.50</td>\n",
       "      <td>22393905.60</td>\n",
       "      <td>11.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-29</th>\n",
       "      <td>19.10</td>\n",
       "      <td>23.54</td>\n",
       "      <td>23.09</td>\n",
       "      <td>17.80</td>\n",
       "      <td>27925126.0</td>\n",
       "      <td>3.75</td>\n",
       "      <td>19.39</td>\n",
       "      <td>19.018</td>\n",
       "      <td>16.324</td>\n",
       "      <td>15.587</td>\n",
       "      <td>22094764.0</td>\n",
       "      <td>23598847.10</td>\n",
       "      <td>22103348.45</td>\n",
       "      <td>14.39</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high  close    low      volume  price_change  p_change  \\\n",
       "date                                                                         \n",
       "2021-05-07  23.10  24.30  24.05  23.10   1302509.5          0.76      3.26   \n",
       "2021-04-30  22.08  24.23  23.29  19.81  14234763.0          1.28      5.82   \n",
       "2021-03-31  21.54  23.49  22.01  20.28  23400600.0          0.63      2.95   \n",
       "2021-02-26  23.00  25.31  21.38  21.21  22432726.0         -1.71     -7.41   \n",
       "2021-01-29  19.10  23.54  23.09  17.80  27925126.0          3.75     19.39   \n",
       "\n",
       "               ma5    ma10    ma20       v_ma5       v_ma10       v_ma20  \\\n",
       "date                                                                       \n",
       "2021-05-07  22.764  20.090  17.241  17859144.9  20308849.85  20748658.73   \n",
       "2021-04-30  21.822  19.019  16.818  21521885.8  24854576.10  21952860.65   \n",
       "2021-03-31  21.112  17.970  16.361  22789929.6  25258599.60  22737767.70   \n",
       "2021-02-26  20.260  17.069  15.967  22277667.2  24135001.50  22393905.60   \n",
       "2021-01-29  19.018  16.324  15.587  22094764.0  23598847.10  22103348.45   \n",
       "\n",
       "            turnover  \n",
       "date                  \n",
       "2021-05-07      0.67  \n",
       "2021-04-30      7.34  \n",
       "2021-03-31     12.06  \n",
       "2021-02-26     11.56  \n",
       "2021-01-29     14.39  "
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.get_hist_data(\"000001\",ktype=\"m\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "      <th>turnover</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-05-07 15:00:00</th>\n",
       "      <td>24.01</td>\n",
       "      <td>24.03</td>\n",
       "      <td>24.01</td>\n",
       "      <td>23.98</td>\n",
       "      <td>14157.80</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>24.028</td>\n",
       "      <td>23.998</td>\n",
       "      <td>24.0555</td>\n",
       "      <td>13524.9</td>\n",
       "      <td>12686.8</td>\n",
       "      <td>11663.7</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-07 14:55:00</th>\n",
       "      <td>24.03</td>\n",
       "      <td>24.04</td>\n",
       "      <td>24.01</td>\n",
       "      <td>23.96</td>\n",
       "      <td>14808.00</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.08</td>\n",
       "      <td>24.024</td>\n",
       "      <td>24.002</td>\n",
       "      <td>24.0630</td>\n",
       "      <td>12897.5</td>\n",
       "      <td>13060.2</td>\n",
       "      <td>11526.3</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-07 14:50:00</th>\n",
       "      <td>24.08</td>\n",
       "      <td>24.10</td>\n",
       "      <td>24.02</td>\n",
       "      <td>23.96</td>\n",
       "      <td>20615.30</td>\n",
       "      <td>-0.06</td>\n",
       "      <td>-0.25</td>\n",
       "      <td>23.998</td>\n",
       "      <td>24.011</td>\n",
       "      <td>24.0670</td>\n",
       "      <td>12849.7</td>\n",
       "      <td>13332.0</td>\n",
       "      <td>11336.4</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-07 14:45:00</th>\n",
       "      <td>24.01</td>\n",
       "      <td>24.08</td>\n",
       "      <td>24.08</td>\n",
       "      <td>24.01</td>\n",
       "      <td>9387.44</td>\n",
       "      <td>0.07</td>\n",
       "      <td>0.29</td>\n",
       "      <td>23.990</td>\n",
       "      <td>24.012</td>\n",
       "      <td>24.0680</td>\n",
       "      <td>10501.1</td>\n",
       "      <td>12905.3</td>\n",
       "      <td>11421.0</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-07 14:40:00</th>\n",
       "      <td>24.00</td>\n",
       "      <td>24.03</td>\n",
       "      <td>24.02</td>\n",
       "      <td>23.95</td>\n",
       "      <td>8656.08</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.08</td>\n",
       "      <td>23.978</td>\n",
       "      <td>24.012</td>\n",
       "      <td>24.0655</td>\n",
       "      <td>11153.4</td>\n",
       "      <td>12820.4</td>\n",
       "      <td>11752.8</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      open   high  close    low    volume  price_change  \\\n",
       "date                                                                      \n",
       "2021-05-07 15:00:00  24.01  24.03  24.01  23.98  14157.80          0.00   \n",
       "2021-05-07 14:55:00  24.03  24.04  24.01  23.96  14808.00         -0.02   \n",
       "2021-05-07 14:50:00  24.08  24.10  24.02  23.96  20615.30         -0.06   \n",
       "2021-05-07 14:45:00  24.01  24.08  24.08  24.01   9387.44          0.07   \n",
       "2021-05-07 14:40:00  24.00  24.03  24.02  23.95   8656.08          0.02   \n",
       "\n",
       "                     p_change     ma5    ma10     ma20    v_ma5   v_ma10  \\\n",
       "date                                                                       \n",
       "2021-05-07 15:00:00      0.00  24.028  23.998  24.0555  13524.9  12686.8   \n",
       "2021-05-07 14:55:00     -0.08  24.024  24.002  24.0630  12897.5  13060.2   \n",
       "2021-05-07 14:50:00     -0.25  23.998  24.011  24.0670  12849.7  13332.0   \n",
       "2021-05-07 14:45:00      0.29  23.990  24.012  24.0680  10501.1  12905.3   \n",
       "2021-05-07 14:40:00      0.08  23.978  24.012  24.0655  11153.4  12820.4   \n",
       "\n",
       "                      v_ma20  turnover  \n",
       "date                                    \n",
       "2021-05-07 15:00:00  11663.7      0.01  \n",
       "2021-05-07 14:55:00  11526.3      0.01  \n",
       "2021-05-07 14:50:00  11336.4      0.01  \n",
       "2021-05-07 14:45:00  11421.0      0.01  \n",
       "2021-05-07 14:40:00  11752.8      0.01  "
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.get_hist_data(\"000001\",ktype=\"5\").head()  # 5min (15,30,60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>price_change</th>\n",
       "      <th>p_change</th>\n",
       "      <th>ma5</th>\n",
       "      <th>ma10</th>\n",
       "      <th>ma20</th>\n",
       "      <th>v_ma5</th>\n",
       "      <th>v_ma10</th>\n",
       "      <th>v_ma20</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-05-07</th>\n",
       "      <td>3021.87</td>\n",
       "      <td>3026.85</td>\n",
       "      <td>2910.41</td>\n",
       "      <td>2910.41</td>\n",
       "      <td>14489343.0</td>\n",
       "      <td>-104.40</td>\n",
       "      <td>-3.46</td>\n",
       "      <td>3023.724</td>\n",
       "      <td>2992.490</td>\n",
       "      <td>2898.924</td>\n",
       "      <td>16444300.8</td>\n",
       "      <td>16648376.3</td>\n",
       "      <td>15426893.45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-05-06</th>\n",
       "      <td>3059.58</td>\n",
       "      <td>3059.58</td>\n",
       "      <td>3014.81</td>\n",
       "      <td>2961.99</td>\n",
       "      <td>16552269.0</td>\n",
       "      <td>-76.59</td>\n",
       "      <td>-2.48</td>\n",
       "      <td>3038.848</td>\n",
       "      <td>2991.060</td>\n",
       "      <td>2894.984</td>\n",
       "      <td>16974533.4</td>\n",
       "      <td>16827430.9</td>\n",
       "      <td>15346704.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-30</th>\n",
       "      <td>3040.65</td>\n",
       "      <td>3110.16</td>\n",
       "      <td>3091.40</td>\n",
       "      <td>3037.31</td>\n",
       "      <td>16606502.0</td>\n",
       "      <td>39.98</td>\n",
       "      <td>1.31</td>\n",
       "      <td>3029.766</td>\n",
       "      <td>2979.452</td>\n",
       "      <td>2886.855</td>\n",
       "      <td>17752532.8</td>\n",
       "      <td>17050663.6</td>\n",
       "      <td>15277921.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-29</th>\n",
       "      <td>3053.74</td>\n",
       "      <td>3068.60</td>\n",
       "      <td>3051.42</td>\n",
       "      <td>3014.55</td>\n",
       "      <td>16647988.0</td>\n",
       "      <td>0.84</td>\n",
       "      <td>0.03</td>\n",
       "      <td>3010.384</td>\n",
       "      <td>2948.649</td>\n",
       "      <td>2873.055</td>\n",
       "      <td>17834241.6</td>\n",
       "      <td>16717391.8</td>\n",
       "      <td>15101499.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-28</th>\n",
       "      <td>2990.70</td>\n",
       "      <td>3052.04</td>\n",
       "      <td>3050.58</td>\n",
       "      <td>2980.65</td>\n",
       "      <td>17925402.0</td>\n",
       "      <td>64.55</td>\n",
       "      <td>2.16</td>\n",
       "      <td>2987.978</td>\n",
       "      <td>2922.571</td>\n",
       "      <td>2858.409</td>\n",
       "      <td>17635713.4</td>\n",
       "      <td>16185039.8</td>\n",
       "      <td>14900826.45</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               open     high    close      low      volume  price_change  \\\n",
       "date                                                                       \n",
       "2021-05-07  3021.87  3026.85  2910.41  2910.41  14489343.0       -104.40   \n",
       "2021-05-06  3059.58  3059.58  3014.81  2961.99  16552269.0        -76.59   \n",
       "2021-04-30  3040.65  3110.16  3091.40  3037.31  16606502.0         39.98   \n",
       "2021-04-29  3053.74  3068.60  3051.42  3014.55  16647988.0          0.84   \n",
       "2021-04-28  2990.70  3052.04  3050.58  2980.65  17925402.0         64.55   \n",
       "\n",
       "            p_change       ma5      ma10      ma20       v_ma5      v_ma10  \\\n",
       "date                                                                         \n",
       "2021-05-07     -3.46  3023.724  2992.490  2898.924  16444300.8  16648376.3   \n",
       "2021-05-06     -2.48  3038.848  2991.060  2894.984  16974533.4  16827430.9   \n",
       "2021-04-30      1.31  3029.766  2979.452  2886.855  17752532.8  17050663.6   \n",
       "2021-04-29      0.03  3010.384  2948.649  2873.055  17834241.6  16717391.8   \n",
       "2021-04-28      2.16  2987.978  2922.571  2858.409  17635713.4  16185039.8   \n",
       "\n",
       "                 v_ma20  \n",
       "date                     \n",
       "2021-05-07  15426893.45  \n",
       "2021-05-06  15346704.55  \n",
       "2021-04-30  15277921.70  \n",
       "2021-04-29  15101499.00  \n",
       "2021-04-28  14900826.45  "
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sh上证指数； sz深圳成指； hs300沪深300； sz50上证50； zxb中小板指数； cyb创业板指数\n",
    "ts.get_hist_data(\"cyb\").head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.股票数据获取（巨宽数据源）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "from jqdatasdk import *\n",
    "\n",
    "auth('18280180192', 'Tencent123')  # 请自行前往 https://www.joinquant.com/ 免费申请TOKEN\n",
    "\n",
    "security = '000300.XSHG'\n",
    "start_date = '2020-01-01'\n",
    "end_date = '2020-03-01'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>4121.35</td>\n",
       "      <td>4152.24</td>\n",
       "      <td>4172.66</td>\n",
       "      <td>4121.35</td>\n",
       "      <td>1.821168e+10</td>\n",
       "      <td>2.701055e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>4161.22</td>\n",
       "      <td>4144.96</td>\n",
       "      <td>4164.30</td>\n",
       "      <td>4131.86</td>\n",
       "      <td>1.428262e+10</td>\n",
       "      <td>2.152163e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-06</th>\n",
       "      <td>4120.52</td>\n",
       "      <td>4129.30</td>\n",
       "      <td>4170.64</td>\n",
       "      <td>4102.38</td>\n",
       "      <td>1.753100e+10</td>\n",
       "      <td>2.501821e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-07</th>\n",
       "      <td>4137.40</td>\n",
       "      <td>4160.23</td>\n",
       "      <td>4161.25</td>\n",
       "      <td>4135.10</td>\n",
       "      <td>1.394890e+10</td>\n",
       "      <td>1.963891e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-08</th>\n",
       "      <td>4139.63</td>\n",
       "      <td>4112.32</td>\n",
       "      <td>4149.81</td>\n",
       "      <td>4101.98</td>\n",
       "      <td>1.675858e+10</td>\n",
       "      <td>2.124063e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               open    close     high      low        volume         money\n",
       "2020-01-02  4121.35  4152.24  4172.66  4121.35  1.821168e+10  2.701055e+11\n",
       "2020-01-03  4161.22  4144.96  4164.30  4131.86  1.428262e+10  2.152163e+11\n",
       "2020-01-06  4120.52  4129.30  4170.64  4102.38  1.753100e+10  2.501821e+11\n",
       "2020-01-07  4137.40  4160.23  4161.25  4135.10  1.394890e+10  1.963891e+11\n",
       "2020-01-08  4139.63  4112.32  4149.81  4101.98  1.675858e+10  2.124063e+11"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# security 股票代码\n",
    "# frequency 时间粒度（1d=日）\n",
    "# skip_paused 是否跳过缺失交易数据时间点\n",
    "stock_price = get_price(security=security, start_date=start_date, end_date=end_date, frequency='1d',skip_paused=False)\n",
    "\n",
    "stock_price.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.股票数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.1 对数收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200228</td>\n",
       "      <td>14.85</td>\n",
       "      <td>15.04</td>\n",
       "      <td>14.46</td>\n",
       "      <td>14.50</td>\n",
       "      <td>15.11</td>\n",
       "      <td>-0.61</td>\n",
       "      <td>-4.0371</td>\n",
       "      <td>1300644.45</td>\n",
       "      <td>1906892.413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200227</td>\n",
       "      <td>14.96</td>\n",
       "      <td>15.15</td>\n",
       "      <td>14.89</td>\n",
       "      <td>15.11</td>\n",
       "      <td>14.99</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.8005</td>\n",
       "      <td>975270.90</td>\n",
       "      <td>1464605.739</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200226</td>\n",
       "      <td>14.77</td>\n",
       "      <td>15.27</td>\n",
       "      <td>14.70</td>\n",
       "      <td>14.99</td>\n",
       "      <td>15.04</td>\n",
       "      <td>-0.05</td>\n",
       "      <td>-0.3324</td>\n",
       "      <td>1176599.15</td>\n",
       "      <td>1769612.245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200225</td>\n",
       "      <td>15.00</td>\n",
       "      <td>15.13</td>\n",
       "      <td>14.78</td>\n",
       "      <td>15.04</td>\n",
       "      <td>15.23</td>\n",
       "      <td>-0.19</td>\n",
       "      <td>-1.2475</td>\n",
       "      <td>1144575.02</td>\n",
       "      <td>1710369.786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200224</td>\n",
       "      <td>15.46</td>\n",
       "      <td>15.46</td>\n",
       "      <td>15.15</td>\n",
       "      <td>15.23</td>\n",
       "      <td>15.58</td>\n",
       "      <td>-0.35</td>\n",
       "      <td>-2.2465</td>\n",
       "      <td>1191794.50</td>\n",
       "      <td>1820183.854</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "0  000001.SZ   20200228  14.85  15.04  14.46  14.50      15.11   -0.61   \n",
       "1  000001.SZ   20200227  14.96  15.15  14.89  15.11      14.99    0.12   \n",
       "2  000001.SZ   20200226  14.77  15.27  14.70  14.99      15.04   -0.05   \n",
       "3  000001.SZ   20200225  15.00  15.13  14.78  15.04      15.23   -0.19   \n",
       "4  000001.SZ   20200224  15.46  15.46  15.15  15.23      15.58   -0.35   \n",
       "\n",
       "   pct_chg         vol       amount  \n",
       "0  -4.0371  1300644.45  1906892.413  \n",
       "1   0.8005   975270.90  1464605.739  \n",
       "2  -0.3324  1176599.15  1769612.245  \n",
       "3  -1.2475  1144575.02  1710369.786  \n",
       "4  -2.2465  1191794.50  1820183.854  "
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取2支股票的历史价格时间序列\n",
    "start_date = '20200101'\n",
    "end_date = '20200301'\n",
    "ts_1_code = '000001.SZ'\n",
    "ts_2_code = '000004.SZ'\n",
    "\n",
    "\n",
    "stock_1_price = pro.query('daily', ts_code=ts_1_code, start_date=start_date, end_date=end_date)\n",
    "\n",
    "stock_2_price = pro.query('daily', ts_code=ts_2_code, start_date=start_date, end_date=end_date)\n",
    "\n",
    "stock_1_price.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "      <th>index</th>\n",
       "      <th>next_close</th>\n",
       "      <th>logreturn</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200102</td>\n",
       "      <td>16.65</td>\n",
       "      <td>16.95</td>\n",
       "      <td>16.55</td>\n",
       "      <td>16.87</td>\n",
       "      <td>16.45</td>\n",
       "      <td>0.42</td>\n",
       "      <td>2.5532</td>\n",
       "      <td>1530231.87</td>\n",
       "      <td>2571196.482</td>\n",
       "      <td>0</td>\n",
       "      <td>17.18</td>\n",
       "      <td>0.018209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200103</td>\n",
       "      <td>16.94</td>\n",
       "      <td>17.31</td>\n",
       "      <td>16.92</td>\n",
       "      <td>17.18</td>\n",
       "      <td>16.87</td>\n",
       "      <td>0.31</td>\n",
       "      <td>1.8376</td>\n",
       "      <td>1116194.81</td>\n",
       "      <td>1914495.474</td>\n",
       "      <td>0</td>\n",
       "      <td>17.07</td>\n",
       "      <td>-0.006423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200106</td>\n",
       "      <td>17.01</td>\n",
       "      <td>17.34</td>\n",
       "      <td>16.91</td>\n",
       "      <td>17.07</td>\n",
       "      <td>17.18</td>\n",
       "      <td>-0.11</td>\n",
       "      <td>-0.6403</td>\n",
       "      <td>862083.50</td>\n",
       "      <td>1477930.193</td>\n",
       "      <td>0</td>\n",
       "      <td>17.15</td>\n",
       "      <td>0.004676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200107</td>\n",
       "      <td>17.13</td>\n",
       "      <td>17.28</td>\n",
       "      <td>16.95</td>\n",
       "      <td>17.15</td>\n",
       "      <td>17.07</td>\n",
       "      <td>0.08</td>\n",
       "      <td>0.4687</td>\n",
       "      <td>728607.56</td>\n",
       "      <td>1247047.135</td>\n",
       "      <td>0</td>\n",
       "      <td>16.66</td>\n",
       "      <td>-0.028988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200108</td>\n",
       "      <td>17.00</td>\n",
       "      <td>17.05</td>\n",
       "      <td>16.63</td>\n",
       "      <td>16.66</td>\n",
       "      <td>17.15</td>\n",
       "      <td>-0.49</td>\n",
       "      <td>-2.8571</td>\n",
       "      <td>847824.12</td>\n",
       "      <td>1423608.811</td>\n",
       "      <td>0</td>\n",
       "      <td>16.79</td>\n",
       "      <td>0.007773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200109</td>\n",
       "      <td>16.81</td>\n",
       "      <td>16.93</td>\n",
       "      <td>16.53</td>\n",
       "      <td>16.79</td>\n",
       "      <td>16.66</td>\n",
       "      <td>0.13</td>\n",
       "      <td>0.7803</td>\n",
       "      <td>1031636.65</td>\n",
       "      <td>1725326.806</td>\n",
       "      <td>0</td>\n",
       "      <td>16.69</td>\n",
       "      <td>-0.005974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200110</td>\n",
       "      <td>16.79</td>\n",
       "      <td>16.81</td>\n",
       "      <td>16.52</td>\n",
       "      <td>16.69</td>\n",
       "      <td>16.79</td>\n",
       "      <td>-0.10</td>\n",
       "      <td>-0.5956</td>\n",
       "      <td>585548.45</td>\n",
       "      <td>975154.818</td>\n",
       "      <td>0</td>\n",
       "      <td>16.99</td>\n",
       "      <td>0.017815</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200113</td>\n",
       "      <td>16.75</td>\n",
       "      <td>17.03</td>\n",
       "      <td>16.61</td>\n",
       "      <td>16.99</td>\n",
       "      <td>16.69</td>\n",
       "      <td>0.30</td>\n",
       "      <td>1.7975</td>\n",
       "      <td>872133.36</td>\n",
       "      <td>1468271.683</td>\n",
       "      <td>0</td>\n",
       "      <td>16.76</td>\n",
       "      <td>-0.013630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200114</td>\n",
       "      <td>16.99</td>\n",
       "      <td>17.27</td>\n",
       "      <td>16.76</td>\n",
       "      <td>16.76</td>\n",
       "      <td>16.99</td>\n",
       "      <td>-0.23</td>\n",
       "      <td>-1.3537</td>\n",
       "      <td>1304493.66</td>\n",
       "      <td>2217608.852</td>\n",
       "      <td>0</td>\n",
       "      <td>16.52</td>\n",
       "      <td>-0.014423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200115</td>\n",
       "      <td>16.79</td>\n",
       "      <td>16.86</td>\n",
       "      <td>16.45</td>\n",
       "      <td>16.52</td>\n",
       "      <td>16.76</td>\n",
       "      <td>-0.24</td>\n",
       "      <td>-1.4320</td>\n",
       "      <td>859439.12</td>\n",
       "      <td>1424889.228</td>\n",
       "      <td>0</td>\n",
       "      <td>16.33</td>\n",
       "      <td>-0.011568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200116</td>\n",
       "      <td>16.52</td>\n",
       "      <td>16.57</td>\n",
       "      <td>16.20</td>\n",
       "      <td>16.33</td>\n",
       "      <td>16.52</td>\n",
       "      <td>-0.19</td>\n",
       "      <td>-1.1501</td>\n",
       "      <td>1028104.67</td>\n",
       "      <td>1678888.507</td>\n",
       "      <td>0</td>\n",
       "      <td>16.39</td>\n",
       "      <td>0.003667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200117</td>\n",
       "      <td>16.38</td>\n",
       "      <td>16.55</td>\n",
       "      <td>16.35</td>\n",
       "      <td>16.39</td>\n",
       "      <td>16.33</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.3674</td>\n",
       "      <td>605436.69</td>\n",
       "      <td>995909.007</td>\n",
       "      <td>0</td>\n",
       "      <td>16.45</td>\n",
       "      <td>0.003654</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200120</td>\n",
       "      <td>16.43</td>\n",
       "      <td>16.61</td>\n",
       "      <td>16.35</td>\n",
       "      <td>16.45</td>\n",
       "      <td>16.39</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.3661</td>\n",
       "      <td>746074.75</td>\n",
       "      <td>1226464.649</td>\n",
       "      <td>0</td>\n",
       "      <td>16.00</td>\n",
       "      <td>-0.027737</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200121</td>\n",
       "      <td>16.34</td>\n",
       "      <td>16.34</td>\n",
       "      <td>15.93</td>\n",
       "      <td>16.00</td>\n",
       "      <td>16.45</td>\n",
       "      <td>-0.45</td>\n",
       "      <td>-2.7356</td>\n",
       "      <td>896603.10</td>\n",
       "      <td>1442171.431</td>\n",
       "      <td>0</td>\n",
       "      <td>16.09</td>\n",
       "      <td>0.005609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200122</td>\n",
       "      <td>15.92</td>\n",
       "      <td>16.16</td>\n",
       "      <td>15.71</td>\n",
       "      <td>16.09</td>\n",
       "      <td>16.00</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.5625</td>\n",
       "      <td>719464.91</td>\n",
       "      <td>1150933.398</td>\n",
       "      <td>0</td>\n",
       "      <td>15.54</td>\n",
       "      <td>-0.034781</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200123</td>\n",
       "      <td>15.92</td>\n",
       "      <td>15.92</td>\n",
       "      <td>15.39</td>\n",
       "      <td>15.54</td>\n",
       "      <td>16.09</td>\n",
       "      <td>-0.55</td>\n",
       "      <td>-3.4183</td>\n",
       "      <td>1100592.07</td>\n",
       "      <td>1723394.336</td>\n",
       "      <td>0</td>\n",
       "      <td>13.99</td>\n",
       "      <td>-0.105075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200203</td>\n",
       "      <td>13.99</td>\n",
       "      <td>14.70</td>\n",
       "      <td>13.99</td>\n",
       "      <td>13.99</td>\n",
       "      <td>15.54</td>\n",
       "      <td>-1.55</td>\n",
       "      <td>-9.9743</td>\n",
       "      <td>2259194.83</td>\n",
       "      <td>3201454.164</td>\n",
       "      <td>0</td>\n",
       "      <td>14.60</td>\n",
       "      <td>0.042679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200204</td>\n",
       "      <td>14.05</td>\n",
       "      <td>14.66</td>\n",
       "      <td>14.02</td>\n",
       "      <td>14.60</td>\n",
       "      <td>13.99</td>\n",
       "      <td>0.61</td>\n",
       "      <td>4.3603</td>\n",
       "      <td>1706172.07</td>\n",
       "      <td>2442932.842</td>\n",
       "      <td>0</td>\n",
       "      <td>14.63</td>\n",
       "      <td>0.002053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200205</td>\n",
       "      <td>14.59</td>\n",
       "      <td>14.89</td>\n",
       "      <td>14.32</td>\n",
       "      <td>14.63</td>\n",
       "      <td>14.60</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.2055</td>\n",
       "      <td>1491380.21</td>\n",
       "      <td>2177632.043</td>\n",
       "      <td>0</td>\n",
       "      <td>14.77</td>\n",
       "      <td>0.009524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200206</td>\n",
       "      <td>14.81</td>\n",
       "      <td>14.87</td>\n",
       "      <td>14.51</td>\n",
       "      <td>14.77</td>\n",
       "      <td>14.63</td>\n",
       "      <td>0.14</td>\n",
       "      <td>0.9569</td>\n",
       "      <td>1185815.72</td>\n",
       "      <td>1740107.625</td>\n",
       "      <td>0</td>\n",
       "      <td>14.62</td>\n",
       "      <td>-0.010208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200207</td>\n",
       "      <td>14.60</td>\n",
       "      <td>14.69</td>\n",
       "      <td>14.41</td>\n",
       "      <td>14.62</td>\n",
       "      <td>14.77</td>\n",
       "      <td>-0.15</td>\n",
       "      <td>-1.0156</td>\n",
       "      <td>924852.96</td>\n",
       "      <td>1345053.255</td>\n",
       "      <td>0</td>\n",
       "      <td>14.50</td>\n",
       "      <td>-0.008242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200210</td>\n",
       "      <td>14.51</td>\n",
       "      <td>14.53</td>\n",
       "      <td>14.30</td>\n",
       "      <td>14.50</td>\n",
       "      <td>14.62</td>\n",
       "      <td>-0.12</td>\n",
       "      <td>-0.8208</td>\n",
       "      <td>1339495.24</td>\n",
       "      <td>1931983.482</td>\n",
       "      <td>0</td>\n",
       "      <td>14.79</td>\n",
       "      <td>0.019803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200211</td>\n",
       "      <td>14.60</td>\n",
       "      <td>14.94</td>\n",
       "      <td>14.56</td>\n",
       "      <td>14.79</td>\n",
       "      <td>14.50</td>\n",
       "      <td>0.29</td>\n",
       "      <td>2.0000</td>\n",
       "      <td>1407507.44</td>\n",
       "      <td>2077194.138</td>\n",
       "      <td>0</td>\n",
       "      <td>14.77</td>\n",
       "      <td>-0.001353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200212</td>\n",
       "      <td>14.79</td>\n",
       "      <td>14.82</td>\n",
       "      <td>14.60</td>\n",
       "      <td>14.77</td>\n",
       "      <td>14.79</td>\n",
       "      <td>-0.02</td>\n",
       "      <td>-0.1352</td>\n",
       "      <td>1070503.21</td>\n",
       "      <td>1573229.042</td>\n",
       "      <td>0</td>\n",
       "      <td>14.65</td>\n",
       "      <td>-0.008158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200213</td>\n",
       "      <td>14.71</td>\n",
       "      <td>14.88</td>\n",
       "      <td>14.61</td>\n",
       "      <td>14.65</td>\n",
       "      <td>14.77</td>\n",
       "      <td>-0.12</td>\n",
       "      <td>-0.8125</td>\n",
       "      <td>1013205.28</td>\n",
       "      <td>1491327.713</td>\n",
       "      <td>0</td>\n",
       "      <td>15.03</td>\n",
       "      <td>0.025608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200214</td>\n",
       "      <td>14.75</td>\n",
       "      <td>15.14</td>\n",
       "      <td>14.70</td>\n",
       "      <td>15.03</td>\n",
       "      <td>14.65</td>\n",
       "      <td>0.38</td>\n",
       "      <td>2.5939</td>\n",
       "      <td>1512434.73</td>\n",
       "      <td>2253906.452</td>\n",
       "      <td>0</td>\n",
       "      <td>15.37</td>\n",
       "      <td>0.022369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200217</td>\n",
       "      <td>15.04</td>\n",
       "      <td>15.37</td>\n",
       "      <td>14.93</td>\n",
       "      <td>15.37</td>\n",
       "      <td>15.03</td>\n",
       "      <td>0.34</td>\n",
       "      <td>2.2621</td>\n",
       "      <td>1543696.01</td>\n",
       "      <td>2337993.586</td>\n",
       "      <td>0</td>\n",
       "      <td>15.20</td>\n",
       "      <td>-0.011122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200218</td>\n",
       "      <td>15.33</td>\n",
       "      <td>15.33</td>\n",
       "      <td>15.01</td>\n",
       "      <td>15.20</td>\n",
       "      <td>15.37</td>\n",
       "      <td>-0.17</td>\n",
       "      <td>-1.1061</td>\n",
       "      <td>973612.35</td>\n",
       "      <td>1478274.222</td>\n",
       "      <td>0</td>\n",
       "      <td>15.24</td>\n",
       "      <td>0.002628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200219</td>\n",
       "      <td>15.10</td>\n",
       "      <td>15.37</td>\n",
       "      <td>15.08</td>\n",
       "      <td>15.24</td>\n",
       "      <td>15.20</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.2632</td>\n",
       "      <td>874106.93</td>\n",
       "      <td>1333730.218</td>\n",
       "      <td>0</td>\n",
       "      <td>15.59</td>\n",
       "      <td>0.022706</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200220</td>\n",
       "      <td>15.27</td>\n",
       "      <td>15.62</td>\n",
       "      <td>15.10</td>\n",
       "      <td>15.59</td>\n",
       "      <td>15.24</td>\n",
       "      <td>0.35</td>\n",
       "      <td>2.2966</td>\n",
       "      <td>1235444.34</td>\n",
       "      <td>1897923.029</td>\n",
       "      <td>0</td>\n",
       "      <td>15.58</td>\n",
       "      <td>-0.000642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200221</td>\n",
       "      <td>15.49</td>\n",
       "      <td>15.72</td>\n",
       "      <td>15.45</td>\n",
       "      <td>15.58</td>\n",
       "      <td>15.59</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>-0.0641</td>\n",
       "      <td>995071.02</td>\n",
       "      <td>1546692.930</td>\n",
       "      <td>0</td>\n",
       "      <td>15.23</td>\n",
       "      <td>-0.022721</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200224</td>\n",
       "      <td>15.46</td>\n",
       "      <td>15.46</td>\n",
       "      <td>15.15</td>\n",
       "      <td>15.23</td>\n",
       "      <td>15.58</td>\n",
       "      <td>-0.35</td>\n",
       "      <td>-2.2465</td>\n",
       "      <td>1191794.50</td>\n",
       "      <td>1820183.854</td>\n",
       "      <td>0</td>\n",
       "      <td>15.04</td>\n",
       "      <td>-0.012554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200225</td>\n",
       "      <td>15.00</td>\n",
       "      <td>15.13</td>\n",
       "      <td>14.78</td>\n",
       "      <td>15.04</td>\n",
       "      <td>15.23</td>\n",
       "      <td>-0.19</td>\n",
       "      <td>-1.2475</td>\n",
       "      <td>1144575.02</td>\n",
       "      <td>1710369.786</td>\n",
       "      <td>0</td>\n",
       "      <td>14.99</td>\n",
       "      <td>-0.003330</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200226</td>\n",
       "      <td>14.77</td>\n",
       "      <td>15.27</td>\n",
       "      <td>14.70</td>\n",
       "      <td>14.99</td>\n",
       "      <td>15.04</td>\n",
       "      <td>-0.05</td>\n",
       "      <td>-0.3324</td>\n",
       "      <td>1176599.15</td>\n",
       "      <td>1769612.245</td>\n",
       "      <td>0</td>\n",
       "      <td>15.11</td>\n",
       "      <td>0.007973</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000001.SZ</td>\n",
       "      <td>20200227</td>\n",
       "      <td>14.96</td>\n",
       "      <td>15.15</td>\n",
       "      <td>14.89</td>\n",
       "      <td>15.11</td>\n",
       "      <td>14.99</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.8005</td>\n",
       "      <td>975270.90</td>\n",
       "      <td>1464605.739</td>\n",
       "      <td>0</td>\n",
       "      <td>14.50</td>\n",
       "      <td>-0.041208</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "35  000001.SZ   20200102  16.65  16.95  16.55  16.87      16.45    0.42   \n",
       "34  000001.SZ   20200103  16.94  17.31  16.92  17.18      16.87    0.31   \n",
       "33  000001.SZ   20200106  17.01  17.34  16.91  17.07      17.18   -0.11   \n",
       "32  000001.SZ   20200107  17.13  17.28  16.95  17.15      17.07    0.08   \n",
       "31  000001.SZ   20200108  17.00  17.05  16.63  16.66      17.15   -0.49   \n",
       "30  000001.SZ   20200109  16.81  16.93  16.53  16.79      16.66    0.13   \n",
       "29  000001.SZ   20200110  16.79  16.81  16.52  16.69      16.79   -0.10   \n",
       "28  000001.SZ   20200113  16.75  17.03  16.61  16.99      16.69    0.30   \n",
       "27  000001.SZ   20200114  16.99  17.27  16.76  16.76      16.99   -0.23   \n",
       "26  000001.SZ   20200115  16.79  16.86  16.45  16.52      16.76   -0.24   \n",
       "25  000001.SZ   20200116  16.52  16.57  16.20  16.33      16.52   -0.19   \n",
       "24  000001.SZ   20200117  16.38  16.55  16.35  16.39      16.33    0.06   \n",
       "23  000001.SZ   20200120  16.43  16.61  16.35  16.45      16.39    0.06   \n",
       "22  000001.SZ   20200121  16.34  16.34  15.93  16.00      16.45   -0.45   \n",
       "21  000001.SZ   20200122  15.92  16.16  15.71  16.09      16.00    0.09   \n",
       "20  000001.SZ   20200123  15.92  15.92  15.39  15.54      16.09   -0.55   \n",
       "19  000001.SZ   20200203  13.99  14.70  13.99  13.99      15.54   -1.55   \n",
       "18  000001.SZ   20200204  14.05  14.66  14.02  14.60      13.99    0.61   \n",
       "17  000001.SZ   20200205  14.59  14.89  14.32  14.63      14.60    0.03   \n",
       "16  000001.SZ   20200206  14.81  14.87  14.51  14.77      14.63    0.14   \n",
       "15  000001.SZ   20200207  14.60  14.69  14.41  14.62      14.77   -0.15   \n",
       "14  000001.SZ   20200210  14.51  14.53  14.30  14.50      14.62   -0.12   \n",
       "13  000001.SZ   20200211  14.60  14.94  14.56  14.79      14.50    0.29   \n",
       "12  000001.SZ   20200212  14.79  14.82  14.60  14.77      14.79   -0.02   \n",
       "11  000001.SZ   20200213  14.71  14.88  14.61  14.65      14.77   -0.12   \n",
       "10  000001.SZ   20200214  14.75  15.14  14.70  15.03      14.65    0.38   \n",
       "9   000001.SZ   20200217  15.04  15.37  14.93  15.37      15.03    0.34   \n",
       "8   000001.SZ   20200218  15.33  15.33  15.01  15.20      15.37   -0.17   \n",
       "7   000001.SZ   20200219  15.10  15.37  15.08  15.24      15.20    0.04   \n",
       "6   000001.SZ   20200220  15.27  15.62  15.10  15.59      15.24    0.35   \n",
       "5   000001.SZ   20200221  15.49  15.72  15.45  15.58      15.59   -0.01   \n",
       "4   000001.SZ   20200224  15.46  15.46  15.15  15.23      15.58   -0.35   \n",
       "3   000001.SZ   20200225  15.00  15.13  14.78  15.04      15.23   -0.19   \n",
       "2   000001.SZ   20200226  14.77  15.27  14.70  14.99      15.04   -0.05   \n",
       "1   000001.SZ   20200227  14.96  15.15  14.89  15.11      14.99    0.12   \n",
       "\n",
       "    pct_chg         vol       amount index  next_close  logreturn  \n",
       "35   2.5532  1530231.87  2571196.482     0       17.18   0.018209  \n",
       "34   1.8376  1116194.81  1914495.474     0       17.07  -0.006423  \n",
       "33  -0.6403   862083.50  1477930.193     0       17.15   0.004676  \n",
       "32   0.4687   728607.56  1247047.135     0       16.66  -0.028988  \n",
       "31  -2.8571   847824.12  1423608.811     0       16.79   0.007773  \n",
       "30   0.7803  1031636.65  1725326.806     0       16.69  -0.005974  \n",
       "29  -0.5956   585548.45   975154.818     0       16.99   0.017815  \n",
       "28   1.7975   872133.36  1468271.683     0       16.76  -0.013630  \n",
       "27  -1.3537  1304493.66  2217608.852     0       16.52  -0.014423  \n",
       "26  -1.4320   859439.12  1424889.228     0       16.33  -0.011568  \n",
       "25  -1.1501  1028104.67  1678888.507     0       16.39   0.003667  \n",
       "24   0.3674   605436.69   995909.007     0       16.45   0.003654  \n",
       "23   0.3661   746074.75  1226464.649     0       16.00  -0.027737  \n",
       "22  -2.7356   896603.10  1442171.431     0       16.09   0.005609  \n",
       "21   0.5625   719464.91  1150933.398     0       15.54  -0.034781  \n",
       "20  -3.4183  1100592.07  1723394.336     0       13.99  -0.105075  \n",
       "19  -9.9743  2259194.83  3201454.164     0       14.60   0.042679  \n",
       "18   4.3603  1706172.07  2442932.842     0       14.63   0.002053  \n",
       "17   0.2055  1491380.21  2177632.043     0       14.77   0.009524  \n",
       "16   0.9569  1185815.72  1740107.625     0       14.62  -0.010208  \n",
       "15  -1.0156   924852.96  1345053.255     0       14.50  -0.008242  \n",
       "14  -0.8208  1339495.24  1931983.482     0       14.79   0.019803  \n",
       "13   2.0000  1407507.44  2077194.138     0       14.77  -0.001353  \n",
       "12  -0.1352  1070503.21  1573229.042     0       14.65  -0.008158  \n",
       "11  -0.8125  1013205.28  1491327.713     0       15.03   0.025608  \n",
       "10   2.5939  1512434.73  2253906.452     0       15.37   0.022369  \n",
       "9    2.2621  1543696.01  2337993.586     0       15.20  -0.011122  \n",
       "8   -1.1061   973612.35  1478274.222     0       15.24   0.002628  \n",
       "7    0.2632   874106.93  1333730.218     0       15.59   0.022706  \n",
       "6    2.2966  1235444.34  1897923.029     0       15.58  -0.000642  \n",
       "5   -0.0641   995071.02  1546692.930     0       15.23  -0.022721  \n",
       "4   -2.2465  1191794.50  1820183.854     0       15.04  -0.012554  \n",
       "3   -1.2475  1144575.02  1710369.786     0       14.99  -0.003330  \n",
       "2   -0.3324  1176599.15  1769612.245     0       15.11   0.007973  \n",
       "1    0.8005   975270.90  1464605.739     0       14.50  -0.041208  "
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算000001.SZ的对数收益\n",
    "\n",
    "stock_1_price = stock_1_price[::-1]  # 按时间升序展示\n",
    "\n",
    "stock_1_price['index'] = ['0'] * len(stock_1_price['close'])  # 若不存在index则使用该方法填充\n",
    "stock_1_price['next_close'] = stock_1_price.groupby('index')['close'].shift(-1)  # 生成next close值\n",
    "stock_1_price = stock_1_price.drop(index=stock_1_price.index[-1])  # 删除最后一交易日（因不存在next close值）\n",
    "\n",
    "logreturn = []\n",
    "for i in stock_1_price.index:\n",
    "    logreturn.append(math.log(stock_1_price['next_close'][i]/stock_1_price['close'][i]))\n",
    "\n",
    "stock_1_price['logreturn'] = logreturn\n",
    "\n",
    "stock_1_price"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.2 股票的对数收益率的相关性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "def multipl(a,b):\n",
    "    sumofab = 0.0\n",
    "    for i in range(len(a)):\n",
    "        temp = a[i] * b[i]\n",
    "        sumofab += temp\n",
    "    return sumofab\n",
    " \n",
    "def corrcoef(x,y):\n",
    "    n = len(x)\n",
    "    \n",
    "    #求和\n",
    "    sum1 = sum(x)\n",
    "    sum2 = sum(y)\n",
    "    \n",
    "    #求乘积之和\n",
    "    sumofxy = multipl(x,y)\n",
    "    \n",
    "    #求平方和\n",
    "    sumofx2 = sum([pow(i,2) for i in x])\n",
    "    sumofy2 = sum([pow(j,2) for j in y])\n",
    "    num = sumofxy-(float(sum1)*float(sum2)/n)\n",
    "    \n",
    "    den = math.sqrt((sumofx2-float(sum1**2)/n)*(sumofy2-float(sum2**2)/n))\n",
    "    \n",
    "    return num/den"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算stock_1_price和stock_1_price的皮尔逊相关系数\n",
    "corrcoef(list(stock_1_price['logreturn']), list(stock_1_price['logreturn']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "      <th>index</th>\n",
       "      <th>next_close</th>\n",
       "      <th>logreturn</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200102</td>\n",
       "      <td>22.65</td>\n",
       "      <td>22.90</td>\n",
       "      <td>22.17</td>\n",
       "      <td>22.33</td>\n",
       "      <td>22.59</td>\n",
       "      <td>-0.26</td>\n",
       "      <td>-1.1510</td>\n",
       "      <td>17853.20</td>\n",
       "      <td>40144.977</td>\n",
       "      <td>0</td>\n",
       "      <td>22.35</td>\n",
       "      <td>0.000895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200103</td>\n",
       "      <td>22.31</td>\n",
       "      <td>22.48</td>\n",
       "      <td>22.03</td>\n",
       "      <td>22.35</td>\n",
       "      <td>22.33</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.0896</td>\n",
       "      <td>13380.09</td>\n",
       "      <td>29779.317</td>\n",
       "      <td>0</td>\n",
       "      <td>21.75</td>\n",
       "      <td>-0.027213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200106</td>\n",
       "      <td>22.20</td>\n",
       "      <td>22.20</td>\n",
       "      <td>21.55</td>\n",
       "      <td>21.75</td>\n",
       "      <td>22.35</td>\n",
       "      <td>-0.60</td>\n",
       "      <td>-2.6846</td>\n",
       "      <td>20316.99</td>\n",
       "      <td>44304.874</td>\n",
       "      <td>0</td>\n",
       "      <td>22.10</td>\n",
       "      <td>0.015964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200107</td>\n",
       "      <td>21.80</td>\n",
       "      <td>22.20</td>\n",
       "      <td>21.67</td>\n",
       "      <td>22.10</td>\n",
       "      <td>21.75</td>\n",
       "      <td>0.35</td>\n",
       "      <td>1.6092</td>\n",
       "      <td>17433.20</td>\n",
       "      <td>38299.735</td>\n",
       "      <td>0</td>\n",
       "      <td>21.78</td>\n",
       "      <td>-0.014585</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200108</td>\n",
       "      <td>21.91</td>\n",
       "      <td>22.77</td>\n",
       "      <td>21.78</td>\n",
       "      <td>21.78</td>\n",
       "      <td>22.10</td>\n",
       "      <td>-0.32</td>\n",
       "      <td>-1.4480</td>\n",
       "      <td>15503.78</td>\n",
       "      <td>34574.063</td>\n",
       "      <td>0</td>\n",
       "      <td>22.32</td>\n",
       "      <td>0.024491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200109</td>\n",
       "      <td>22.11</td>\n",
       "      <td>22.50</td>\n",
       "      <td>21.80</td>\n",
       "      <td>22.32</td>\n",
       "      <td>21.78</td>\n",
       "      <td>0.54</td>\n",
       "      <td>2.4793</td>\n",
       "      <td>12415.32</td>\n",
       "      <td>27618.678</td>\n",
       "      <td>0</td>\n",
       "      <td>22.33</td>\n",
       "      <td>0.000448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200110</td>\n",
       "      <td>22.50</td>\n",
       "      <td>22.80</td>\n",
       "      <td>22.15</td>\n",
       "      <td>22.33</td>\n",
       "      <td>22.32</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.0448</td>\n",
       "      <td>17227.00</td>\n",
       "      <td>38594.631</td>\n",
       "      <td>0</td>\n",
       "      <td>23.03</td>\n",
       "      <td>0.030867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200113</td>\n",
       "      <td>22.50</td>\n",
       "      <td>23.70</td>\n",
       "      <td>22.33</td>\n",
       "      <td>23.03</td>\n",
       "      <td>22.33</td>\n",
       "      <td>0.70</td>\n",
       "      <td>3.1348</td>\n",
       "      <td>35126.48</td>\n",
       "      <td>80788.795</td>\n",
       "      <td>0</td>\n",
       "      <td>23.00</td>\n",
       "      <td>-0.001303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200114</td>\n",
       "      <td>22.96</td>\n",
       "      <td>23.36</td>\n",
       "      <td>22.75</td>\n",
       "      <td>23.00</td>\n",
       "      <td>23.03</td>\n",
       "      <td>-0.03</td>\n",
       "      <td>-0.1303</td>\n",
       "      <td>19488.16</td>\n",
       "      <td>44925.565</td>\n",
       "      <td>0</td>\n",
       "      <td>22.55</td>\n",
       "      <td>-0.019759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200115</td>\n",
       "      <td>23.02</td>\n",
       "      <td>23.02</td>\n",
       "      <td>22.35</td>\n",
       "      <td>22.55</td>\n",
       "      <td>23.00</td>\n",
       "      <td>-0.45</td>\n",
       "      <td>-1.9565</td>\n",
       "      <td>14543.01</td>\n",
       "      <td>32867.949</td>\n",
       "      <td>0</td>\n",
       "      <td>22.46</td>\n",
       "      <td>-0.003999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200116</td>\n",
       "      <td>22.81</td>\n",
       "      <td>22.81</td>\n",
       "      <td>22.13</td>\n",
       "      <td>22.46</td>\n",
       "      <td>22.55</td>\n",
       "      <td>-0.09</td>\n",
       "      <td>-0.3991</td>\n",
       "      <td>13060.55</td>\n",
       "      <td>29221.955</td>\n",
       "      <td>0</td>\n",
       "      <td>22.24</td>\n",
       "      <td>-0.009843</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200117</td>\n",
       "      <td>22.30</td>\n",
       "      <td>22.60</td>\n",
       "      <td>22.18</td>\n",
       "      <td>22.24</td>\n",
       "      <td>22.46</td>\n",
       "      <td>-0.22</td>\n",
       "      <td>-0.9795</td>\n",
       "      <td>10451.42</td>\n",
       "      <td>23375.135</td>\n",
       "      <td>0</td>\n",
       "      <td>23.33</td>\n",
       "      <td>0.047848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200120</td>\n",
       "      <td>22.68</td>\n",
       "      <td>23.60</td>\n",
       "      <td>22.60</td>\n",
       "      <td>23.33</td>\n",
       "      <td>22.24</td>\n",
       "      <td>1.09</td>\n",
       "      <td>4.9011</td>\n",
       "      <td>28042.52</td>\n",
       "      <td>65069.386</td>\n",
       "      <td>0</td>\n",
       "      <td>23.55</td>\n",
       "      <td>0.009386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200121</td>\n",
       "      <td>23.47</td>\n",
       "      <td>23.98</td>\n",
       "      <td>22.98</td>\n",
       "      <td>23.55</td>\n",
       "      <td>23.33</td>\n",
       "      <td>0.22</td>\n",
       "      <td>0.9430</td>\n",
       "      <td>27734.09</td>\n",
       "      <td>65161.618</td>\n",
       "      <td>0</td>\n",
       "      <td>23.70</td>\n",
       "      <td>0.006349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200122</td>\n",
       "      <td>23.51</td>\n",
       "      <td>24.18</td>\n",
       "      <td>23.03</td>\n",
       "      <td>23.70</td>\n",
       "      <td>23.55</td>\n",
       "      <td>0.15</td>\n",
       "      <td>0.6369</td>\n",
       "      <td>25890.01</td>\n",
       "      <td>61303.986</td>\n",
       "      <td>0</td>\n",
       "      <td>22.82</td>\n",
       "      <td>-0.037838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200123</td>\n",
       "      <td>23.01</td>\n",
       "      <td>23.80</td>\n",
       "      <td>22.45</td>\n",
       "      <td>22.82</td>\n",
       "      <td>23.70</td>\n",
       "      <td>-0.88</td>\n",
       "      <td>-3.7131</td>\n",
       "      <td>26265.72</td>\n",
       "      <td>60624.416</td>\n",
       "      <td>0</td>\n",
       "      <td>20.54</td>\n",
       "      <td>-0.105263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200203</td>\n",
       "      <td>20.54</td>\n",
       "      <td>20.54</td>\n",
       "      <td>20.54</td>\n",
       "      <td>20.54</td>\n",
       "      <td>22.82</td>\n",
       "      <td>-2.28</td>\n",
       "      <td>-9.9912</td>\n",
       "      <td>4300.00</td>\n",
       "      <td>8832.200</td>\n",
       "      <td>0</td>\n",
       "      <td>19.73</td>\n",
       "      <td>-0.040234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200204</td>\n",
       "      <td>18.51</td>\n",
       "      <td>20.10</td>\n",
       "      <td>18.51</td>\n",
       "      <td>19.73</td>\n",
       "      <td>20.54</td>\n",
       "      <td>-0.81</td>\n",
       "      <td>-3.9435</td>\n",
       "      <td>32157.10</td>\n",
       "      <td>62466.364</td>\n",
       "      <td>0</td>\n",
       "      <td>21.12</td>\n",
       "      <td>0.068080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200205</td>\n",
       "      <td>19.71</td>\n",
       "      <td>21.36</td>\n",
       "      <td>19.71</td>\n",
       "      <td>21.12</td>\n",
       "      <td>19.73</td>\n",
       "      <td>1.39</td>\n",
       "      <td>7.0451</td>\n",
       "      <td>28554.50</td>\n",
       "      <td>58674.385</td>\n",
       "      <td>0</td>\n",
       "      <td>21.76</td>\n",
       "      <td>0.029853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200206</td>\n",
       "      <td>21.16</td>\n",
       "      <td>22.18</td>\n",
       "      <td>20.91</td>\n",
       "      <td>21.76</td>\n",
       "      <td>21.12</td>\n",
       "      <td>0.64</td>\n",
       "      <td>3.0303</td>\n",
       "      <td>23748.00</td>\n",
       "      <td>51480.051</td>\n",
       "      <td>0</td>\n",
       "      <td>21.57</td>\n",
       "      <td>-0.008770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200207</td>\n",
       "      <td>21.58</td>\n",
       "      <td>22.03</td>\n",
       "      <td>21.00</td>\n",
       "      <td>21.57</td>\n",
       "      <td>21.76</td>\n",
       "      <td>-0.19</td>\n",
       "      <td>-0.8732</td>\n",
       "      <td>25770.51</td>\n",
       "      <td>55235.268</td>\n",
       "      <td>0</td>\n",
       "      <td>21.90</td>\n",
       "      <td>0.015183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200210</td>\n",
       "      <td>21.78</td>\n",
       "      <td>22.18</td>\n",
       "      <td>21.38</td>\n",
       "      <td>21.90</td>\n",
       "      <td>21.57</td>\n",
       "      <td>0.33</td>\n",
       "      <td>1.5299</td>\n",
       "      <td>22118.18</td>\n",
       "      <td>48091.949</td>\n",
       "      <td>0</td>\n",
       "      <td>21.37</td>\n",
       "      <td>-0.024499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200211</td>\n",
       "      <td>21.82</td>\n",
       "      <td>21.92</td>\n",
       "      <td>21.21</td>\n",
       "      <td>21.37</td>\n",
       "      <td>21.90</td>\n",
       "      <td>-0.53</td>\n",
       "      <td>-2.4201</td>\n",
       "      <td>17746.45</td>\n",
       "      <td>38129.473</td>\n",
       "      <td>0</td>\n",
       "      <td>21.63</td>\n",
       "      <td>0.012093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200212</td>\n",
       "      <td>21.37</td>\n",
       "      <td>21.76</td>\n",
       "      <td>21.10</td>\n",
       "      <td>21.63</td>\n",
       "      <td>21.37</td>\n",
       "      <td>0.26</td>\n",
       "      <td>1.2167</td>\n",
       "      <td>13620.00</td>\n",
       "      <td>29355.418</td>\n",
       "      <td>0</td>\n",
       "      <td>21.30</td>\n",
       "      <td>-0.015374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200213</td>\n",
       "      <td>21.60</td>\n",
       "      <td>21.62</td>\n",
       "      <td>21.25</td>\n",
       "      <td>21.30</td>\n",
       "      <td>21.63</td>\n",
       "      <td>-0.33</td>\n",
       "      <td>-1.5257</td>\n",
       "      <td>13368.01</td>\n",
       "      <td>28599.546</td>\n",
       "      <td>0</td>\n",
       "      <td>21.34</td>\n",
       "      <td>0.001876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200214</td>\n",
       "      <td>21.49</td>\n",
       "      <td>21.78</td>\n",
       "      <td>21.16</td>\n",
       "      <td>21.34</td>\n",
       "      <td>21.30</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.1878</td>\n",
       "      <td>14508.00</td>\n",
       "      <td>31093.681</td>\n",
       "      <td>0</td>\n",
       "      <td>23.47</td>\n",
       "      <td>0.095140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200217</td>\n",
       "      <td>22.05</td>\n",
       "      <td>23.47</td>\n",
       "      <td>21.91</td>\n",
       "      <td>23.47</td>\n",
       "      <td>21.34</td>\n",
       "      <td>2.13</td>\n",
       "      <td>9.9813</td>\n",
       "      <td>29755.20</td>\n",
       "      <td>68272.475</td>\n",
       "      <td>0</td>\n",
       "      <td>25.82</td>\n",
       "      <td>0.095426</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200218</td>\n",
       "      <td>24.88</td>\n",
       "      <td>25.82</td>\n",
       "      <td>23.70</td>\n",
       "      <td>25.82</td>\n",
       "      <td>23.47</td>\n",
       "      <td>2.35</td>\n",
       "      <td>10.0128</td>\n",
       "      <td>65499.14</td>\n",
       "      <td>162710.522</td>\n",
       "      <td>0</td>\n",
       "      <td>28.40</td>\n",
       "      <td>0.095240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200219</td>\n",
       "      <td>26.84</td>\n",
       "      <td>28.40</td>\n",
       "      <td>26.84</td>\n",
       "      <td>28.40</td>\n",
       "      <td>25.82</td>\n",
       "      <td>2.58</td>\n",
       "      <td>9.9923</td>\n",
       "      <td>28798.11</td>\n",
       "      <td>80680.600</td>\n",
       "      <td>0</td>\n",
       "      <td>31.24</td>\n",
       "      <td>0.095310</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200220</td>\n",
       "      <td>29.37</td>\n",
       "      <td>31.24</td>\n",
       "      <td>25.56</td>\n",
       "      <td>31.24</td>\n",
       "      <td>28.40</td>\n",
       "      <td>2.84</td>\n",
       "      <td>10.0000</td>\n",
       "      <td>150919.04</td>\n",
       "      <td>435957.820</td>\n",
       "      <td>0</td>\n",
       "      <td>34.36</td>\n",
       "      <td>0.095194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200221</td>\n",
       "      <td>34.00</td>\n",
       "      <td>34.36</td>\n",
       "      <td>33.00</td>\n",
       "      <td>34.36</td>\n",
       "      <td>31.24</td>\n",
       "      <td>3.12</td>\n",
       "      <td>9.9872</td>\n",
       "      <td>63137.42</td>\n",
       "      <td>215454.458</td>\n",
       "      <td>0</td>\n",
       "      <td>37.80</td>\n",
       "      <td>0.095416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200224</td>\n",
       "      <td>37.80</td>\n",
       "      <td>37.80</td>\n",
       "      <td>37.80</td>\n",
       "      <td>37.80</td>\n",
       "      <td>34.36</td>\n",
       "      <td>3.44</td>\n",
       "      <td>10.0116</td>\n",
       "      <td>14243.09</td>\n",
       "      <td>53838.880</td>\n",
       "      <td>0</td>\n",
       "      <td>41.58</td>\n",
       "      <td>0.095310</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200225</td>\n",
       "      <td>40.00</td>\n",
       "      <td>41.58</td>\n",
       "      <td>36.70</td>\n",
       "      <td>41.58</td>\n",
       "      <td>37.80</td>\n",
       "      <td>3.78</td>\n",
       "      <td>10.0000</td>\n",
       "      <td>279365.39</td>\n",
       "      <td>1111662.193</td>\n",
       "      <td>0</td>\n",
       "      <td>37.42</td>\n",
       "      <td>-0.105414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200226</td>\n",
       "      <td>38.21</td>\n",
       "      <td>39.55</td>\n",
       "      <td>37.42</td>\n",
       "      <td>37.42</td>\n",
       "      <td>41.58</td>\n",
       "      <td>-4.16</td>\n",
       "      <td>-10.0048</td>\n",
       "      <td>141673.94</td>\n",
       "      <td>540539.274</td>\n",
       "      <td>0</td>\n",
       "      <td>37.00</td>\n",
       "      <td>-0.011287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000004.SZ</td>\n",
       "      <td>20200227</td>\n",
       "      <td>35.81</td>\n",
       "      <td>39.96</td>\n",
       "      <td>33.93</td>\n",
       "      <td>37.00</td>\n",
       "      <td>37.42</td>\n",
       "      <td>-0.42</td>\n",
       "      <td>-1.1224</td>\n",
       "      <td>192268.71</td>\n",
       "      <td>705851.089</td>\n",
       "      <td>0</td>\n",
       "      <td>40.70</td>\n",
       "      <td>0.095310</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "35  000004.SZ   20200102  22.65  22.90  22.17  22.33      22.59   -0.26   \n",
       "34  000004.SZ   20200103  22.31  22.48  22.03  22.35      22.33    0.02   \n",
       "33  000004.SZ   20200106  22.20  22.20  21.55  21.75      22.35   -0.60   \n",
       "32  000004.SZ   20200107  21.80  22.20  21.67  22.10      21.75    0.35   \n",
       "31  000004.SZ   20200108  21.91  22.77  21.78  21.78      22.10   -0.32   \n",
       "30  000004.SZ   20200109  22.11  22.50  21.80  22.32      21.78    0.54   \n",
       "29  000004.SZ   20200110  22.50  22.80  22.15  22.33      22.32    0.01   \n",
       "28  000004.SZ   20200113  22.50  23.70  22.33  23.03      22.33    0.70   \n",
       "27  000004.SZ   20200114  22.96  23.36  22.75  23.00      23.03   -0.03   \n",
       "26  000004.SZ   20200115  23.02  23.02  22.35  22.55      23.00   -0.45   \n",
       "25  000004.SZ   20200116  22.81  22.81  22.13  22.46      22.55   -0.09   \n",
       "24  000004.SZ   20200117  22.30  22.60  22.18  22.24      22.46   -0.22   \n",
       "23  000004.SZ   20200120  22.68  23.60  22.60  23.33      22.24    1.09   \n",
       "22  000004.SZ   20200121  23.47  23.98  22.98  23.55      23.33    0.22   \n",
       "21  000004.SZ   20200122  23.51  24.18  23.03  23.70      23.55    0.15   \n",
       "20  000004.SZ   20200123  23.01  23.80  22.45  22.82      23.70   -0.88   \n",
       "19  000004.SZ   20200203  20.54  20.54  20.54  20.54      22.82   -2.28   \n",
       "18  000004.SZ   20200204  18.51  20.10  18.51  19.73      20.54   -0.81   \n",
       "17  000004.SZ   20200205  19.71  21.36  19.71  21.12      19.73    1.39   \n",
       "16  000004.SZ   20200206  21.16  22.18  20.91  21.76      21.12    0.64   \n",
       "15  000004.SZ   20200207  21.58  22.03  21.00  21.57      21.76   -0.19   \n",
       "14  000004.SZ   20200210  21.78  22.18  21.38  21.90      21.57    0.33   \n",
       "13  000004.SZ   20200211  21.82  21.92  21.21  21.37      21.90   -0.53   \n",
       "12  000004.SZ   20200212  21.37  21.76  21.10  21.63      21.37    0.26   \n",
       "11  000004.SZ   20200213  21.60  21.62  21.25  21.30      21.63   -0.33   \n",
       "10  000004.SZ   20200214  21.49  21.78  21.16  21.34      21.30    0.04   \n",
       "9   000004.SZ   20200217  22.05  23.47  21.91  23.47      21.34    2.13   \n",
       "8   000004.SZ   20200218  24.88  25.82  23.70  25.82      23.47    2.35   \n",
       "7   000004.SZ   20200219  26.84  28.40  26.84  28.40      25.82    2.58   \n",
       "6   000004.SZ   20200220  29.37  31.24  25.56  31.24      28.40    2.84   \n",
       "5   000004.SZ   20200221  34.00  34.36  33.00  34.36      31.24    3.12   \n",
       "4   000004.SZ   20200224  37.80  37.80  37.80  37.80      34.36    3.44   \n",
       "3   000004.SZ   20200225  40.00  41.58  36.70  41.58      37.80    3.78   \n",
       "2   000004.SZ   20200226  38.21  39.55  37.42  37.42      41.58   -4.16   \n",
       "1   000004.SZ   20200227  35.81  39.96  33.93  37.00      37.42   -0.42   \n",
       "\n",
       "    pct_chg        vol       amount index  next_close  logreturn  \n",
       "35  -1.1510   17853.20    40144.977     0       22.35   0.000895  \n",
       "34   0.0896   13380.09    29779.317     0       21.75  -0.027213  \n",
       "33  -2.6846   20316.99    44304.874     0       22.10   0.015964  \n",
       "32   1.6092   17433.20    38299.735     0       21.78  -0.014585  \n",
       "31  -1.4480   15503.78    34574.063     0       22.32   0.024491  \n",
       "30   2.4793   12415.32    27618.678     0       22.33   0.000448  \n",
       "29   0.0448   17227.00    38594.631     0       23.03   0.030867  \n",
       "28   3.1348   35126.48    80788.795     0       23.00  -0.001303  \n",
       "27  -0.1303   19488.16    44925.565     0       22.55  -0.019759  \n",
       "26  -1.9565   14543.01    32867.949     0       22.46  -0.003999  \n",
       "25  -0.3991   13060.55    29221.955     0       22.24  -0.009843  \n",
       "24  -0.9795   10451.42    23375.135     0       23.33   0.047848  \n",
       "23   4.9011   28042.52    65069.386     0       23.55   0.009386  \n",
       "22   0.9430   27734.09    65161.618     0       23.70   0.006349  \n",
       "21   0.6369   25890.01    61303.986     0       22.82  -0.037838  \n",
       "20  -3.7131   26265.72    60624.416     0       20.54  -0.105263  \n",
       "19  -9.9912    4300.00     8832.200     0       19.73  -0.040234  \n",
       "18  -3.9435   32157.10    62466.364     0       21.12   0.068080  \n",
       "17   7.0451   28554.50    58674.385     0       21.76   0.029853  \n",
       "16   3.0303   23748.00    51480.051     0       21.57  -0.008770  \n",
       "15  -0.8732   25770.51    55235.268     0       21.90   0.015183  \n",
       "14   1.5299   22118.18    48091.949     0       21.37  -0.024499  \n",
       "13  -2.4201   17746.45    38129.473     0       21.63   0.012093  \n",
       "12   1.2167   13620.00    29355.418     0       21.30  -0.015374  \n",
       "11  -1.5257   13368.01    28599.546     0       21.34   0.001876  \n",
       "10   0.1878   14508.00    31093.681     0       23.47   0.095140  \n",
       "9    9.9813   29755.20    68272.475     0       25.82   0.095426  \n",
       "8   10.0128   65499.14   162710.522     0       28.40   0.095240  \n",
       "7    9.9923   28798.11    80680.600     0       31.24   0.095310  \n",
       "6   10.0000  150919.04   435957.820     0       34.36   0.095194  \n",
       "5    9.9872   63137.42   215454.458     0       37.80   0.095416  \n",
       "4   10.0116   14243.09    53838.880     0       41.58   0.095310  \n",
       "3   10.0000  279365.39  1111662.193     0       37.42  -0.105414  \n",
       "2  -10.0048  141673.94   540539.274     0       37.00  -0.011287  \n",
       "1   -1.1224  192268.71   705851.089     0       40.70   0.095310  "
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算000002.SZ的对数收益\n",
    "\n",
    "stock_2_price = stock_2_price[::-1]  # 按时间升序展示\n",
    "\n",
    "stock_2_price['index'] = ['0'] * len(stock_2_price['close'])  # 若不存在index则使用该方法填充\n",
    "stock_2_price['next_close'] = stock_2_price.groupby('index')['close'].shift(-1)  # 生成next close值\n",
    "stock_2_price = stock_2_price.drop(index=stock_2_price.index[-1])  # 删除最后一交易日（因不存在next close值）\n",
    "\n",
    "logreturn = []\n",
    "for i in stock_2_price.index:\n",
    "    logreturn.append(math.log(stock_2_price['next_close'][i]/stock_2_price['close'][i]))\n",
    "\n",
    "stock_2_price['logreturn'] = logreturn\n",
    "\n",
    "stock_2_price"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 查看时间序列长度是否一致"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(list(stock_1_price['logreturn']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(list(stock_2_price['logreturn']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.24605475817036795"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算stock_1_price和stock_2_price的皮尔逊相关系数\n",
    "corrcoef(list(stock_1_price['logreturn']), list(stock_2_price['logreturn']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
